package com.sjft.cloud.commons.security.config;

import com.sjft.cloud.commons.security.rest.RestAuthExceptionEntryPoint;
import com.sjft.cloud.commons.security.rest.RestOAuth2AccessDeniedHandler;
import com.sjft.cloud.commons.security.rest.RestWebResponseExceptionTranslator;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
import org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint;

/**
 * OAuth2AuthenticationEntryPointConfig
 *
 * @author Jin
 */
@Configuration
public class RestErrorConfig {

    @Bean
    public RestWebResponseExceptionTranslator restWebResponseExceptionTranslator() {
        return new RestWebResponseExceptionTranslator();
    }

    @Bean
    public OAuth2AuthenticationEntryPoint getOAuth2AuthenticationEntryPoint(RestWebResponseExceptionTranslator restWebResponseExceptionTranslator) {
        OAuth2AuthenticationEntryPoint authenticationEntryPoint = new OAuth2AuthenticationEntryPoint();
        authenticationEntryPoint.setExceptionTranslator(restWebResponseExceptionTranslator);
        return authenticationEntryPoint;
    }


    @Bean
    public RestAuthExceptionEntryPoint getRestAuthExceptionEntryPoint(ObjectMapper objectMapper) {
        return new RestAuthExceptionEntryPoint(objectMapper);
    }

    @Bean
    public OAuth2AccessDeniedHandler restOAuth2AccessDeniedHandler() {
        return new RestOAuth2AccessDeniedHandler();
    }
}
